<?php
declare(strict_types=1);

namespace app\controller;

use app\common\Backend;
use app\service\RoleService;


class Role extends Backend
{
    public function initialize()
    {
        parent::initialize();
        $this->service = new RoleService();
    }

    //新增
    public function add()
    {
        $param = request()->param();
        //将数组转化成字符串以“,”分割
        $param['rules'] = implode(',', $param['rules']);
        $result = $this->service->add($param);
        if($result){
            return success(null, '添加成功');
        }else{
            return error('添加失败');
        }
    }

    //添加角色组（权限路由）查询getRoles方法，请求该接口时会将所有角色组返回，并将该角色组所有权限路由在routes项中返回
    public function getRoles()
    {
        $result = $this->service->roleWithRoutes();
        if($result){
            return success($result, 'success');
        }else{
            return error('未找到有效数据');
        }
    }
}